using System.Reflection;
using System.Windows;
using System.Windows.Input;
using Vibstudio.NetConfigurator.UI.Util;

namespace Vibstudio.NetConfigurator.UI.ViewModels
{
    using Vibstudio.NetConfigurator.UI.Common;

    public class HeaderViewModel : ViewModelBase
    {
        #region Fields

        private bool _toolbarVisibility;
        private RelayCommand _updateCommand;

        #endregion

        #region Properties

        public string ProductName { get; private set; }

        public string ProductVersion { get; private set; }

        public string OSPlatformVersion { get; private set; }

        public string OSVersionDetails { get; private set; }

        public bool ToolbarVisibility
        {
            get { return _toolbarVisibility; }
            set
            {
                _toolbarVisibility = value;
                OnPropertyChanged("ToolbarVisibility");
            }
        }

        #endregion

        #region Ctor(s)

        public HeaderViewModel()
        {
            _toolbarVisibility = IsToolbarVisible();
            SetProductInformation();
            SetOSInformation();
        }

        #endregion

        #region Commands

        public ICommand Update
        {
            get { return _updateCommand ?? (_updateCommand = new RelayCommand(PromptToUpdate)); }
        }

        #endregion

        #region Private, support methods

        private static bool IsToolbarVisible()
        {
            // Algoritmo di ricerca nuovi aggiornamenti.
            // Se ci sono nuovi aggiornamenti
            // {
            //  return true;
            // }
            // Se non sono nuovi aggiornamenti
            // {
            return false;
            // }
        }

        private void PromptToUpdate(object o)
        {
            MessageBoxResult messageBoxResult = MessageBox.Show("A new update is available. Want to upgrade?", "",
                                                                MessageBoxButton.YesNo,
                                                                MessageBoxImage.Question);
            if (messageBoxResult == MessageBoxResult.Yes) RunUpdate();
            ToolbarVisibility = false;
        }

        private void RunUpdate()
        {
            // 1. Lanciare NetConfiguratorUploader (Console o Windows Form application)

            // 2. Chiudere NetConfigurator
            Application.Current.Shutdown();

            // NetConfiguratorUploader dovrà:
            // a. eseguire l'aggiornamento
            // b. rilanciere NetConfigurator
            // c. chiudere se stesso
        }

        private void SetProductInformation()
        {
            ProductName = GetProductName();
            ProductVersion = GetAssemblyVersion();
        }

        private void SetOSInformation()
        {
            //OSPlatformVersion = OS.GetPlatformVersionDescription(OS.GetPlatformVersion());
            //OSVersionDetails = OS.GetVersionDetails();

            OSPlatformVersion = string.Format("{0} {1}", OSInfo.Caption, OSInfo.OSArchitecture);
            OSVersionDetails = string.Format("v{0}", OSInfo.Version);
        }

        private static string GetProductName()
        {
            Assembly assembly = Assembly.GetExecutingAssembly();
            return assembly.GetName().Name;
        }

        private static string GetAssemblyVersion()
        {
            Assembly assembly = Assembly.GetExecutingAssembly();
            return string.Format("v{0}", assembly.GetName().Version);
        }

        #endregion
    }
}